﻿using Dapper;
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Himp.Platform.Client
{
    public class CodeConversionManager
    {
        private static Random random = new Random();

        public CodeConversionManager()
        {
        }

        /// <summary>
        /// 生成制样码
        /// </summary>
        /// <param name="sorcCode"></param>
        /// <param name="operName"></param>
        /// <returns></returns>
        public Tuple<bool, string> GenerateSamplingNo(string sorcCode, string operName)
        {
            CodeConversion codeConversion = new CodeConversion(sorcCode, 1, operName);
            return GetTargetCode(codeConversion);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sorcCode"></param>
        /// <param name="operName"></param>
        /// <returns></returns>
        public Tuple<bool, string> GenerateTestingNo(string sorcCode, string operName)
        {
            CodeConversion codeConversion = new CodeConversion(sorcCode, 2, operName);
            return GetTargetCode(codeConversion);
        }

        private Tuple<bool,string> GetTargetCode(CodeConversion codeConversion)
        {
            string connectionString = ConfigurationManager.AppSettings["DefaultConnect"];
            string query = @"SELECT TargetCode 
                            FROM [TaskScheduling].TS_CodeConversion
                            WHERE SorcCode = @SorcCode AND ConversionType = @ConversionType";
            string existTargetCodeSql = "SELECT COUNT(*) FROM [TaskScheduling].TS_CodeConversion where TargetCode=@TargetCode";

            string insert = @"INSERT INTO
                            [TaskScheduling].TS_CodeConversion
                            (
                            SorcCode,
                            TargetCode,
                            ConversionType,
                            ConversionTime,
                            OperName
                            )
                            VALUES
                            (
                            @SorcCode,
                            @TargetCode,
                            @ConversionType,
                            @ConversionTime,
                            @OperName
                            )";

            using (IDbConnection connection = new SqlConnection(connectionString))
            {
                connection.Open();
                var targetCode = connection.ExecuteScalar<string>(query,new 
                {
                    codeConversion.SorcCode,
                    codeConversion.ConversionType
                });

                if (string.IsNullOrEmpty(targetCode))
                {
                    int exitTargetFlag = 0;

                    do
                    {
                        if (codeConversion.ConversionType == 1) //制样码
                        {
                            codeConversion.TargetCode = "C" + DateTime.Now.ToString("yyMMdd") + random.Next(1000, 10000).ToString();
                        }
                        else if (codeConversion.ConversionType == 2) //自动化验码
                        {
                            codeConversion.TargetCode = "T" + DateTime.Now.ToString("yyMMdd") + random.Next(1000, 10000).ToString() + "Z" ;
                        }

                        exitTargetFlag = connection.ExecuteScalar<int>(existTargetCodeSql, new { codeConversion.TargetCode });
                    }
                    while (exitTargetFlag != 0);

                    var inserFlag = connection.Execute(insert, codeConversion);
                    targetCode = codeConversion.TargetCode;
                }

                //手工制样用R后缀
                targetCode = targetCode.Replace("Z", "R");

                return new Tuple<bool, string>(true, targetCode);
            }
        }
    }
}
